package org.threadCase7;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

public class callAbleDemo implements Callable<Integer> {
    public static ArrayList<Integer> prize=new ArrayList<>();
    public ArrayList<Integer> money=new ArrayList<>();
    public int totalMoney=0;
    public int maxMoney=0;
    static {
        Collections.addAll(prize,10,5,20,50,100,200,2,300,80,700);
    }
    @Override
    public Integer call() throws Exception {
//        抽奖，没抽完一直抽
        while (true) {
//            单次抽奖逻辑
            synchronized (callAbleDemo.class) {

                if (prize.isEmpty()) {
//                    抽完退出循环
                    System.out.println(Thread.currentThread().getName() + " 抱歉，当前抽奖已经结束。");
                    break;
                } else {
                    Collections.shuffle(prize);
                    int doal = prize.get(0);
                    if (maxMoney < doal) {
                        maxMoney = doal;
                    }
                    money.add(doal);
                    totalMoney += doal;
                    prize.remove(0);
                }
            }

        }
//        返回结果
        synchronized (prize){

            if (money.isEmpty()) {
                System.out.println(Thread.currentThread().getName() + "没有奖，所以没有最大奖项");
                return null;
            } else {
                System.out.println("在此次抽奖过程中，"+Thread.currentThread().getName()+"共产生了"+money.size()+"个奖项");
                System.out.print("分别为：");
                for (int i = 0; i < money.size(); i++) {
                    if (i==money.size()-1){
                        System.out.print(money.get(i));
                    }else {
                        System.out.print(money.get(i)+",");
                    }
                }
                System.out.println("最高奖项为："+maxMoney+"总计金额为"+totalMoney+"元");
                return maxMoney;
            }
        }
    }


}
